﻿/*
 * BFind - A tool for finding football matches for trading on Betfair
 * Copyright 2009, 2010 Magnus Olsson <magnus72(at)gmail.com>
 * 
 * This file is part of BFind.
 * 
 * BFind is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BFind is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BFind.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Collections.Generic;

namespace betfairfind.Rules.SilkBC
{
    class Filter5 : BaseRule
    {

        double MIN_UNDER25_ODDS;
        double MAX_UNDER25_ODDS;
        double MIN_UNDER45_ODDS;
        double MAX_UNDER45_ODDS;

        public Filter5(Dictionary<string, string> config, List<MyMarket> inplaymarkets, Dictionary<long, MarketPricesCompressed> marketprices)
            : base()
        {
            MarketsUsedInside = new string[] { "over/under 2.5 goals", "over/under 4.5 goals" };

            try
            {
                MIN_UNDER25_ODDS = Convert.ToDouble(config["Rule.SilkBC.Filter5.MIN_UNDER25_ODDS"]);
                MAX_UNDER25_ODDS = Convert.ToDouble(config["Rule.SilkBC.Filter5.MAX_UNDER25_ODDS"]);
                MIN_UNDER45_ODDS = Convert.ToDouble(config["Rule.SilkBC.Filter5.MIN_UNDER45_ODDS"]);
                MAX_UNDER45_ODDS = Convert.ToDouble(config["Rule.SilkBC.Filter5.MAX_UNDER45_ODDS"]);
 
            }
            catch (Exception e)
            {
                Logger.LogError(e.ToString(), true, true);
            }

            string criteria = @"" + MIN_UNDER25_ODDS + "/" +
                MAX_UNDER25_ODDS + "/" +
                MIN_UNDER45_ODDS + "/" +
                MAX_UNDER45_ODDS;

            Logger.Log("============================================================================", true, true);
            Logger.Log("Matches qualified for SilkBC Filter 5 (" + criteria + ")", true, true);
            Logger.Log("----------------------------------------------------------------------------", true, true);

            if (!CheckRequiredMarkets()) { return; }

            string[] games = FindSoccerGames.GetMatches();

            int selectionCount = 0;
            foreach (var game in games)
            {
                try
                {

                    MyMarket market_ou25 = FindSoccerGames.GetMarket(game, "over/under 2.5 goals");
                    MarketPricesCompressed mpc_ou25 = marketprices[market_ou25.marketId];
                    MyMarket market_ou45 = FindSoccerGames.GetMarket(game, "over/under 4.5 goals");
                    MarketPricesCompressed mpc_ou45 = marketprices[market_ou45.marketId];

                    double u25odds = mpc_ou25.GetBestBackOdds(0);
                    double u45odds = mpc_ou45.GetBestBackOdds(0);


                    if ((u25odds >= MIN_UNDER25_ODDS && u25odds <= MAX_UNDER25_ODDS) &&
                       (u45odds >= MIN_UNDER45_ODDS && u45odds <= MAX_UNDER45_ODDS))
                    {
                        Logger.Log(market_ou25.marketTime + ", " + BF.convertPathToGame(game) + ", u2.5: " + u25odds + ", u4.5: " + u45odds, true, true);
                        selectionCount++;
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError("Error in game " + game + Environment.NewLine + ex.ToString(), true, false);
                }

            }
            if (selectionCount == 0)
            {
                Logger.Log("No matches", true, true);
            }

        }
    }
}
